const glob = require("glob");
const { countChinese, countWord } = require("./counter");
const { resolve, relative } = require("path");
const fs = require("fs");

// console.log('list',list)
const total = { en: 0, cn: 0 };
new Array(6).fill().forEach((v, i) => {
  console.log("i:", i);
  let sum = getFolderTotal(
    resolve(__dirname, `../w3c/week${i + 1}/**/*.{srt,md}`)
  );
  total.en += sum.en;
  total.cn += sum.cn;
});
console.log("----TOTAL------------------");
console.log(total);

// getFolderTotal(resolve(__dirname, "../w3c/week1/**/*.{srt,md}"));
// getFolderTotal(resolve(__dirname, "../w3c/week2/**/*.{srt,md}"));
// getFolderTotal(resolve(__dirname, "../w3c/week3/**/*.{srt,md}"));
// getFolderTotal(resolve(__dirname, "../w3c/week4/**/*.{srt,md}"));
// getFolderTotal(resolve(__dirname, "../w3c/week5/**/*.{srt,md}"));
// getFolderTotal(resolve(__dirname, "../w3c/week6/**/*.{srt,md}"));

function getFolderTotal(path) {
  const list = glob.sync(path);
  const sum = {
    cn: 0,
    en: 0,
  };

  list.forEach((path) => {
    const data = countFile(path);
    // 详细数据
    // console.log(relative(__dirname, path), data);
    sum.en += data.en;
    sum.cn += data.cn;
  });
  console.log(`
=======================
Total: en: ${sum.en} words    cn:${sum.cn}字
  `);
  return sum;
}

function countFile(path) {
  const str = fs.readFileSync(path).toString();
  const cn = countChinese(str);
  const all = countWord(str);
  return {
    cn,
    en: all - cn,
  };
}
